package com.xxx.DAO;

import com.xxx.util.JDBCUtil;
import com.xxx.entity.Feedback;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class feedbackDao {
    private JDBCUtil util = new JDBCUtil();

    //添加一条反馈
    public int add(String uName,String sex,int age,String email,String phone,String information){
        String sql = "insert into feedback (uName,sex,age,email,phone,information) values(?,?,?,?,?,?)";
        int result = 0;
        PreparedStatement ps = util.createPstmt(sql);
        try {
            ps.setString(1, uName);
            ps.setString(2, sex);
            ps.setInt(3, age);
            ps.setString(4, email);
            ps.setString(5, phone);
            ps.setString(6, information);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }

    //查询反馈
    public List findAll(){
        int result=0;
        String sql="select * from feedback order by id desc";
        ResultSet rs=null;
        PreparedStatement ps=util.createPstmt(sql);
        List list=new ArrayList();
        try {
            rs=ps.executeQuery();
            while (rs.next()) {
                String uName = rs.getString("uName");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String email = rs.getString("email");
                String phone = rs.getString("phone");
                String information = rs.getString("information");
                Feedback feedback = new Feedback(uName, sex, age, email, phone, information);
                list.add(feedback);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs);
        }
        return list;
    }
}
